package com.qfedu.openplateform.manager.controller;

import com.github.pagehelper.PageInfo;
import com.qfedu.openplateform.manager.entity.AjaxMessage;
import com.qfedu.openplateform.manager.entity.ApiMapping;
import com.qfedu.openplateform.manager.entity.TableData;
import com.qfedu.openplateform.manager.service.ApiMappingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RequestMapping("/sys/api_mapping")
@RestController
public class ApiMappingController {

    @Autowired
    private ApiMappingService apiMappingService;

    @RequestMapping("/table")
    public TableData findAllApiMapping(ApiMapping apiMapping, Integer page, Integer limit){

        PageInfo<ApiMapping> pageInfo = apiMappingService.findALlApiMapping(apiMapping,page,limit);

        return new TableData(pageInfo.getTotal(),pageInfo.getList());
    }

    @RequestMapping("/add")
    public AjaxMessage addApiMapping(ApiMapping apiMapping){
        try {
            apiMappingService.addApiMapping(apiMapping);
            return new AjaxMessage(true, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AjaxMessage(false, "添加失败");
    }

    @RequestMapping("/update")
    public AjaxMessage updateApiMapping(ApiMapping apiMapping){
        try {
            apiMappingService.updateApiMapping(apiMapping);
            return new AjaxMessage(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AjaxMessage(false, "修改失败");
    }

    @RequestMapping("/del")
    public AjaxMessage deleteApiMappings(int[] ids){
        try {
            apiMappingService.deleteApiMappings(ids);
            return new AjaxMessage(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AjaxMessage(false, "删除失败");

    }


}
